#import <Foundation/Foundation.h>
#import "FileRecursion.h"


@interface FileRecursion ()

@property (nonatomic, assign) BOOL isFinished;
@property (nonatomic, assign) NSInteger fileIndex;

@end

@implementation FileRecursion

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.fileIndex = 0;
    }
    return self;
}

- (void)startTraversingDirectory:(NSString *)directoryPath {
    self.isFinished = NO;
    [self recursiveTraversal:directoryPath];
}

- (void)recursiveTraversal:(NSString *)directoryPath {
    if (self.isFinished) {
        NSLog(@"递归结束，标记变量指示结束");
        return;
    }

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath:directoryPath]) {
        self.isFinished = YES;
        NSLog(@"递归结束，路径不存在");
        return;
    }

    NSArray *contents = [fileManager contentsOfDirectoryAtPath:directoryPath error:nil];
    BOOL isDirectory = NO;
    for (NSString *item in contents) {
        NSString *itemPath = [directoryPath stringByAppendingPathComponent:item];
        if ([fileManager fileExistsAtPath:itemPath isDirectory:&isDirectory]) {
            if (isDirectory) {
                NSLog(@"fileDir ==> %@",itemPath);
                [self recursiveTraversal:itemPath];
            } else {
                // 处理文件
                NSLog(@"filePath ==> %@",itemPath);
            }
        }
    }
}

@end
